package com.rover.service.system;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.Common;
import com.rover.dao.BaseDao;
import com.rover.entity.system.Role;
import com.rover.entity.system.User;
import com.rover.entity.system.UserRole;
import com.utils.Encrypt;

/**
 * 用户管理类.
 *
 */
@Service("accountService")
@Transactional
public class AccountService{
    private static Logger logger = LoggerFactory.getLogger(AccountService.class);

    @Autowired
    private BaseDao<Role> roleDao;
    
    @Autowired
    private BaseDao<User> userDao;
    
    @Autowired
    private BaseDao<UserRole> userRoleDao;
    
	public BaseDao<Role> getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(BaseDao<Role> roleDao) {
		this.roleDao = roleDao;
	}

	public BaseDao<User> getUserDao() {
		return userDao;
	}

	public void setUserDao(BaseDao<User> userDao) {
		this.userDao = userDao;
	}
	
	public BaseDao<UserRole> getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(BaseDao<UserRole> userRoleDao) {
		this.userRoleDao = userRoleDao;
	}
	
    //初始
	@Transactional
	public void initData() {
		logger.debug("初始化数据");
        if (userDao.count("select count(u) from User u").longValue() != 0) {
            return;
        }
        //设置系统管理员角色
        Role r = new Role();
        r.setName(Common.ADMIN_NAME);
        r.setDescription("系统管理员角色");
        r.setsAdmin("0");
        this.roleDao.save(r);
        
        //设置管理员用户
        User user = new User();
        user.setLoginName(Common.ADMIN_NAME);
        user.setName("管理员");
        //md5加密
        user.setPassword(Encrypt.md5(Common.ADMIN_PASSWORD));
        userDao.save(user);
        
        //设置角色-用户关联表
        UserRole userRole = new UserRole();
        userRole.setTrole(r);
        userRole.setTuser(user);
        userRoleDao.save(userRole);
      
	}
    
   
}
